package com.xgimi.defaultapp;

import java.util.ArrayList;
import java.util.List;

import com.xgimi.defaultapp.adpter.DefaultApater;
import com.xgimi.defaultapp.adpter.InputMethodAdapter;
import com.xgimi.defaultapp.utils.CustomApplicationHelper;
import com.xgimi.defaultapp.view.MainUpView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
 * 默认应用.
 * 
 * @author hailong.qiu
 */
public class DefaultActivity extends Activity {

	private static final String TAG = "DefaultApp";

	private CustomApplicationHelper mCusAppHelper;
	private Context mContext;

	ListView mDefualtListView;
	BaseAdapter mDefaultApater;
	// 其它.
	List<ResolveInfo> mDefaultInfoList = new ArrayList<ResolveInfo>();
	// 输入法.
	List<InputMethodInfo> mInputInfoList = new ArrayList<InputMethodInfo>();
	
	MainUpView mMainUpView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_defualt);
		mContext = getApplicationContext();
		mCusAppHelper = new CustomApplicationHelper(getApplicationContext());
		// 初始化view.
		initAllViews();
		// 初始化数据.
		initAllDatas();
	}

	private void initAllViews() {
		mMainUpView = (MainUpView) findViewById(R.id.mainUpView1);
		mDefualtListView = (ListView) findViewById(R.id.defualt_listview);
		mMainUpView.setUpRectResource(R.drawable.test_rectangle);
		mMainUpView.setShadowResource(R.drawable.item_shadow);
		mDefualtListView.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				if (view != null) {
					mMainUpView.setFocusView(view, 1.0f);
				}
			}
			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
	}

	private Intent mInitIntent;

	private void initAllDatas() {
		Intent intent = getIntent();
		Bundle b = intent.getExtras();
		// 获取传入的参数.
		if (b != null) { // 其它.
			mInitIntent = (Intent) b.get("intent");
			mDefaultInfoList = mCusAppHelper.getSpeAppResolveInfos(mInitIntent);
			mDefaultApater = new DefaultApater(this, mCusAppHelper, mDefaultInfoList);
			mDefualtListView.setAdapter(mDefaultApater);
			mDefualtListView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					// 设置默认应用.
					ResolveInfo info = mDefaultInfoList.get(position);
					mCusAppHelper.clearDefaultApp(mDefaultInfoList);
					mCusAppHelper.setDefaultApplication(mInitIntent, info, mDefaultInfoList);
					mDefaultApater.notifyDataSetChanged();
					quitActivity();
				}
			});
		} else { // 输入法.
			mInputInfoList = mCusAppHelper.getAllInputMethod();
			mDefaultApater = new InputMethodAdapter(mContext, mCusAppHelper, mInputInfoList);
			mDefualtListView.setAdapter(mDefaultApater);
			mDefualtListView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					// 设置默认输入法.
					InputMethodInfo info = mInputInfoList.get(position);
					mCusAppHelper.setDefaultInputMethod(info);
					mDefaultApater.notifyDataSetChanged();
					quitActivity();
				}
			});
		}
	}

	public void quitActivity() {
		startActivity(new Intent(mContext, MainActivity.class));
		Toast.makeText(mContext, "设置成功", Toast.LENGTH_LONG).show();
	}

}
